<template>
  <van-list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <div
      class="content"
      v-for="(item, index) in data"
      :key="index"
      @click="toDetail(item.only_id)"
    >
      <div class="left">
        <p>{{ item.info.title }}</p>
        <div class="bottom-content">
          <span>{{ item.info.name }}</span>
          <span>{{ item.info.behot_time }}</span>
          <span>{{ item.info.display_time }}</span>
        </div>
      </div>
      <div class="rigth">
        <img :src="item.info.image" alt="" />
      </div>
    </div>
  </van-list>
</template>

<script>
import { onBeforeMount, ref } from "vue";
import { get } from "../tool/get";
import { useRouter } from "vue-router";
import { Toast } from "vant";

export default {
  name: "list_",
  setup() {
    let data = ref([]);
    const router = useRouter();
    onBeforeMount(() => {
      get("/data/homeData").then((res) => {
        dataChange(res);
      });
    });
    const loading = ref(false);
    const finished = ref(false);
    const dataChange = (res) => {
      res.data.data.map((item) => {
        item.info.behot_time = new Date(
          item.info.behot_time * 1000
        ).toLocaleDateString();
        item.info.display_time = new Date(
          item.info.display_time * 1000
        ).toLocaleString();
        item.info.display_time = item.info.display_time.slice(
          item.info.display_time.length - 9,
          -3
        );
      });
      data.value = data.value.concat(res.data.data);
    };
    const toDetail = (id) => {
      Toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });
      // console.log(id);
      router.push({ path: `/newsArticles/${id}` });
    };
    const onLoad = async () => {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      await get("/data/homeData").then((res) => {
        dataChange(res);
      });
      // 加载状态结束
      loading.value = false;
      // 数据全部加载完成
    };

    return {
      onLoad,
      loading,
      finished,
      data,
      dataChange,
      toDetail,
    };
  },
};
</script>

<style lang="less" scoped>
.content {
  margin: 10px auto;
  width: 90%;
  border-bottom: 1px solid #dbdbdb;
  height: 70px;
  padding: 10px auto;
  display: flex;
  .left {
    flex: 1;
    font-size: 14px;
    height: 100%;
    p {
      text-align: left;
      // overflow: hidden;
      // text-overflow: ellipsis;
      line-height: 20px;
      margin: 0;
      // display: -webkit-box;
      // -webkit-box-orient: vertical;
      // -webkit-line-clamp: 2;
      height: 60%;
      min-height: 60%;
    }
    .bottom-content {
      justify-content: space-between;
      display: flex;
      padding: 5px 10px 0px 5px;
      span {
        // width: 40px;
        // flex-flow: row wrap;
        max-width: 80px;
        overflow: hidden; // 超出隐藏
        white-space: nowrap; //超出不换行
        text-overflow: ellipsis; // 超出打点
        line-height: 20px;
        font-size: 10px;
        text-align: center;
        margin: 0 2px;
      }
    }
  }
  .rigth {
    width: 100px;
    height: 85%;
    img {
      width: 100%;
      height: 100%;
      background: #eee;
      border-radius: 5px;
    }
  }
}
</style>